package com.withouther.robot.rainingrobot.core.config;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.util.WebAppRootListener;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 * @ClassName WebSocketConfig
 * @Auther: tzq
 * @Date: 2020/6/10 08:42
 * @Description:
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class WebSocketConfig implements ServletContextInitializer {
    //    private static Logger log = Logger.getLogger(WebSocketConfig.class);
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        try {
            System.out.println("WebSocketConfig::websAudioAsrServiceImpltocket set params::org.apache.tomcat.websocket set params");
            servletContext.addListener(WebAppRootListener.class);

            //线程池的核心大小。如果不设置，则默认为0
            servletContext.setInitParameter("org.apache.tomcat.websocket.executorCoreSize","100");
            System.out.println("WebSocketConfig::webstocket set params::org.apache.tomcat.websocket set executorCoreSize:100");
            //        //线程池所允许的最大值。如果不设置，则默认为200
            servletContext.setInitParameter("org.apache.tomcat.websocket.executorMaxSize","800");
            System.out.println("WebSocketConfig::webstocket set params::org.apache.tomcat.websocket set executorMaxSize:800");
            //        //线程池中空闲进程所保留的最大时间。如果未指定，则默认为60秒。
            servletContext.setInitParameter("org.apache.tomcat.websocket.executorKeepAliveTimeSeconds","80000");
            System.out.println("WebSocketConfig::webstocket set params::org.apache.tomcat.websocket set executorKeepAliveTimeSeconds:80000");
            //        //写入超时时间
            servletContext.setInitParameter("org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT","90000");
            System.out.println("WebSocketConfig::webstocket set params::org.apache.tomcat.websocket set BLOCKING_SEND_TIMEOUT:90000");
            servletContext.setInitParameter("org.apache.tomcat.websocket.DEFAULT_BUFFER_SIZE","9024000");
            System.out.println("WebSocketConfig::webstocket set params::org.apache.tomcat.websocket set DEFAULT_BUFFER_SIZE:9024000");
            servletContext.setInitParameter("org.apache.tomcat.websocket.textBufferSize","9024000");
            System.out.println("WebSocketConfig::webstocket set params::org.apache.tomcat.websocket set textBufferSize:9024000");
            //二进制信息默认的缓存大小
            servletContext.setInitParameter("org.apache.tomcat.websocket.binaryBufferSize","9024000");
            System.out.println("WebSocketConfig::webstocket set params::org.apache.tomcat.websocket set binaryBufferSize:9024000");

            //时时间的、单位为毫秒的字符串(String)。默认值为5000(5秒)。
            servletContext.setInitParameter("org.apache.tomcat.websocket.IO_TIMEOUT_MS","10000");
            System.out.println("WebSocketConfig::webstocket set params::org.apache.tomcat.websocket set IO_TIMEOUT_MS:10000");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("WebSocketConfig::webstocket set params::org.apache.tomcat.websocket set params" + e.getMessage());
        }
    }
}
